package com.xuzz.xrpc.registry;


import com.xuzz.xrpc.cluster.ILoadbalance;
import com.xuzz.xrpc.cluster.LoadbalanceFactory;
import com.xuzz.xrpc.common.ConfigManager;
import com.xuzz.xrpc.common.PropertyConfig;

import java.util.List;

/**
 * @author far.liu
 */
public abstract class AbstractRegistrar implements IRegistry {
    protected static final String FOLDER = "/xrpc-registrys";
    protected static final String SEPARATOR = "/";

    public AbstractRegistrar()
    {
        String registerAddress = ConfigManager.propertyConfig.getRegistryAddress();
        this.init(registerAddress);
    }

    @Override
    public String discover(String service) {
        List<String> providers = lookup(service);
        ILoadbalance balancer = LoadbalanceFactory.getLoadbalance(ConfigManager.propertyConfig.getLoadBalance());
        String select = balancer.select(providers);
        return select;
    }

    protected abstract void init(String address);

    protected abstract List<String> lookup(String service);
}
